package com.soujiayi.util;

import java.io.BufferedReader;
import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.soujiayi.model.SystemInfo;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;

public class SystemInfoUtil {

	private static SystemInfo sysInfo = new SystemInfo();

	public static SystemInfo getSysInfo(Context context) {

		try {
			JSONObject hardware = new JSONObject();
			JSONObject cpu = new JSONObject();
			cpu.put("cpu_name", getCpuName());
			cpu.put("cpu_minfreq", getMinCpuFreq());
			cpu.put("cpu_maxfreq", getMaxCpuFreq());
			hardware.put("cpu", cpu);
			JSONObject memory = new JSONObject();
			memory.put("ram_space", getTotalRamSize());
			memory.put("rom_space", getTotalRomSize());
			hardware.put("memory_space", memory);
			hardware.put("sd_space", getSDCardMemory());
			hardware.put("mac_address", getMACAddress(context));
			sysInfo.setCellnum(getCellNumber(context));
			sysInfo.setOs_ver(getVersion().toString());
			sysInfo.setHardware(hardware.toString());
			sysInfo.setOther("");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sysInfo;
	}

	/*
	 * get cell phone number
	 */
	public static String getCellNumber(Context context) {
		String result = "";
		String iso = "";
		String phoneNum = "";
		try {
			TelephonyManager tManager = (TelephonyManager) context

			.getSystemService(Context.TELEPHONY_SERVICE);
			iso = tManager.getNetworkCountryIso();
			phoneNum = tManager.getLine1Number();
		} catch (Exception e) {

		}
		result = iso + "_" + phoneNum;
		return result;
	}

	/*
	 * get max cpu frequency
	 */
	public static String getMaxCpuFreq() {
		String result = "NA";
		ProcessBuilder cmd;
		try {
			String[] args = { "/system/bin/cat",
					"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
			cmd = new ProcessBuilder(args);
			Process process = cmd.start();
			InputStream in = process.getInputStream();
			byte[] re = new byte[24];
			while (in.read(re) != -1) {
				result = result + new String(re);
			}
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return result.trim();
	}

	/*
	 * get min cpu frequency
	 */
	public static String getMinCpuFreq() {
		String result = "NA";
		ProcessBuilder cmd;
		try {
			String[] args = { "/system/bin/cat",
					"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
			cmd = new ProcessBuilder(args);
			Process process = cmd.start();
			InputStream in = process.getInputStream();
			byte[] re = new byte[24];
			while (in.read(re) != -1) {
				result = result + new String(re);
			}
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return result.trim();
	}

	/*
	 * get max cpu name
	 */
	public static String getCpuName() {
		String result = "NA";
		try {
			FileReader fr = new FileReader("/proc/cpuinfo");
			BufferedReader br = new BufferedReader(fr);
			String text = br.readLine();
			String[] array = text.split(":\\s+", 2);
			for (int i = 0; i < array.length; i++) {
			}
			result = array[1];
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * get total Ram Size GB
	 */
	public static String getTotalRamSize() {
		String path = "/proc/meminfo";
		String content = "NA";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(path), 8);
			String line;
			if ((line = br.readLine()) != null) {
				content = line;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		// beginIndex
		int begin = content.indexOf(':');
		// endIndex
		int end = content.indexOf('k');
		// 截取字符串信息

		content = content.substring(begin + 1, end).trim();
		return String.valueOf(Long.valueOf(content) / 1024) + "MB";
	}

	/*
	 * get total Rom Size MB
	 */
	public static String getTotalRomSize() {
		String result = "NA";
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		result = String.valueOf(totalBlocks * blockSize / (1024 * 1024)) + "MB";
		return result;
	}

	/*
	 * get total sd card space MB
	 */
	public static String getSDCardMemory() {
		String result = "NA";
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			File sdcardDir = Environment.getExternalStorageDirectory();
			StatFs sf = new StatFs(sdcardDir.getPath());
			long bSize = sf.getBlockSize();
			long bCount = sf.getBlockCount();
			result = String.valueOf(bSize * bCount / (1024 * 1024)); // MB

		}
		return result + "MB";
	}

	/*
	 * get system version
	 */
	public static String getVersion() {
		String result = "NA";
		result = Build.VERSION.RELEASE + "/" + Build.VERSION.SDK;
		return result;
	}

	public static String getMACAddress(Context context) {
		String result = "NA";
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		if (wifiInfo.getMacAddress() != null) {
			result = wifiInfo.getMacAddress();
		}
		return result;
	}

}
